package com.web.config;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.web.controller.IndexController;
import com.web.controller.LoginController;
import com.web.controller.RegistController;
import com.web.interceptor.AuthInterceptor;
import com.web.model.User;

/**
 * 功能：项目jfinal框架基础配置类
 * @author chenxm
 *
 */
public class WebConfig extends JFinalConfig {
	
	private final Log log = LogFactory.getLog(WebConfig.class);
	
	/**
	 * 功能：配置常量
	 */
	public void configConstant(Constants me) {
		me.setDevMode(true);
		me.setError404View("/view/error/404.html");
		me.setError500View("view/error/500.html");		
	}
	
	/**
	 * 功能：配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);
		me.add("/login", LoginController.class);
		me.add("/regist",RegistController.class);
	}
	
	/**
	 * 功能：配置插件
	 */
	public void configPlugin(Plugins me) {
		/*
		// 配置C3p0数据库连接池插件
		C3p0Plugin c3p0Plugin = new C3p0Plugin("jdbc:mysql://127.0.0.1/webdemo", "root", "1q2w3e4r");
		me.add(c3p0Plugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
		me.add(arp);
		*/
		// DruidPlugin
		DruidPlugin dp = new DruidPlugin("jdbc:mysql://127.0.0.1/webdemo", "root", "1q2w3e4r");
		dp.addFilter(new StatFilter());
		WallFilter wall = new WallFilter();
		wall.setDbType("mysql");
		dp.addFilter(wall);
		me.add(dp);
		
		//ActiveRecordPlugin
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		me.add(arp);		
		
		//进行table到model映射配置 
		arp.addMapping("user", User.class);
	}
	
	/**
	 * 功能：配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		me.add(new AuthInterceptor());
		me.add(new SessionInViewInterceptor());//session拦截器，用于在View模板中取出session值 
	}
	
	/**
	 * 功能：配置处理器
	 */
	public void configHandler(Handlers me) {
		//add druid handler
		DruidStatViewHandler dvh = new DruidStatViewHandler("/druid");
		me.add(dvh);		
	}
	
	/**
	 * 功能：web应用启动前调用
	 */
	public void afterJFinalStart() {
		log.debug("webdemo应用开始启动完成...");
	}
	
	/**
	 * 功能：web应用关闭前调用
	 */
	public void beforeJFinalStop() {
		log.debug("webdemo应用准备关闭...");
	}
}
